import baostock as bs
import pandas as pd

# 登录
lg = bs.login()
if lg.error_code != '0':
    raise Exception("baostock login failed")

# 获取指数日线数据
# 中证红利低波100指数 930955
rs = bs.query_history_k_data_plus(
    code="sz.930955",   # sh 开头是上海交易所指数
    fields="date,open,high,low,close,volume",
    start_date="2010-01-01",
    end_date="2025-11-12",
    frequency="d"
)

data_list = []
while rs.next():
    data_list.append(rs.get_row_data())
df = pd.DataFrame(data_list, columns=rs.fields)
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)

print(df.head())

# 登出
bs.logout()
